﻿using Kendo.Mvc.UI;
using Kendo.Mvc.Extensions;
using System;
using System.Linq;
using System.Web.Mvc;
using Microsoft.AspNet.Identity.EntityFramework;
using TelerikedIn.Data;
using TelerikedIn.WebApp.Areas.Administration.Models;

namespace TelerikedIn.WebApp.Areas.Administration.Controllers
{
    [Authorize(Roles = "Administrator")]
    public class UsersController : Controller
    {
        private readonly IUowData data;

        public UsersController()
        {
            this.data = new UowData();
        }

        public UsersController(IUowData data)
        {
            this.data = data;
        }

        public ActionResult Index()
        {
            return View();
        }

        public JsonResult AllUsers([DataSourceRequest] DataSourceRequest request)
        {
            var users = data.Users.All();
            if (User != null)
            {
                users = users.Where(u => u.UserName != User.Identity.Name);
            }

            var userModels = users.Select(UserDetailsVM.FromUser).ToList();

            return Json(userModels.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult Update([DataSourceRequest] DataSourceRequest request, UserDetailsVM user)
        {
            if (user != null && ModelState.IsValid)
            {
                var userToUpdate = data.Users.GetById(user.Id);

                if (userToUpdate != null)
                {
                    userToUpdate.UserName = user.UserName;
                    userToUpdate.FirstName = user.FirstName;
                    userToUpdate.LastName = user.LastName;
                    userToUpdate.City = user.City;
                    userToUpdate.Country = user.Country;
                    userToUpdate.Education = user.Education;
                    userToUpdate.Image = user.Image;
                    userToUpdate.Management.DisableSignIn = user.IsBanned;

                    if (user.IsAdmin)
                    {
                        var adminRole = data.Roles.All().First(r => r.Name == "Administrator");
                        userToUpdate.Roles.Clear();
                        userToUpdate.Roles.Add(new UserRole() { Role = adminRole });
                    }
                    else
                    {
                        var userRole = data.Roles.All().First(r => r.Name == "User");
                        userToUpdate.Roles.Clear();
                        userToUpdate.Roles.Add(new UserRole() { Role = userRole });
                    }

                    data.Users.Update(userToUpdate);
                    data.SaveChanges();
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "User not found.");
                }
            }
            else
            {
                ModelState.AddModelError(string.Empty, "");
            }
            return Json(new[] { user }.ToDataSourceResult(request, ModelState), JsonRequestBehavior.AllowGet);
        }
    }
}